package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.LRechargeMapper;
import com.ruoyi.system.domain.LRecharge;
import com.ruoyi.system.service.ILRechargeService;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;
import static com.ruoyi.system.utils.Code.getCode;
import static com.ruoyi.system.utils.Code.getNowDate;

/**
 * 充值订单Service业务层处理
 *
 * @author zyb
 * @date 2022-01-02
 */
@Service
public class LRechargeServiceImpl extends ServiceImpl<LRechargeMapper, LRecharge> implements ILRechargeService {
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private ISysConfigService sysConfigService;

    /**
     * 查询充值订单
     *
     * @param rechargeid 充值订单主键
     * @return 充值订单
     */
    @Override
    public LRecharge selectLRechargeByRechargeid(Long rechargeid) {
        return baseMapper.selectLRechargeByRechargeid(rechargeid);
    }

    /**
     * 查询充值订单列表
     *
     * @param lRecharge 充值订单
     * @return 充值订单
     */
    @Override
    public List<LRecharge> selectLRechargeList(LRecharge lRecharge) {
        return baseMapper.selectLRechargeList(lRecharge);
    }

    /**
     * 新增充值订单
     *
     * @param lRecharge 充值订单
     * @return 结果
     */
    @Override
    public int insertLRecharge(LRecharge lRecharge) {

        if (!StringUtils.isNotBlank(lRecharge.getBillcode())) {
            lRecharge.setBillcode(String.valueOf(getBillCode()));
        }
        checkBillCode(lRecharge);
        String str = getNowDate();
        int rechargebill = redisCache.getCacheObject("rechargebill" + str) == null ? 0 : redisCache.getCacheObject("rechargebill" + str);
        lRecharge.setCreateBy(String.valueOf(getUserId()));
        lRecharge.setCreateTime(DateUtils.getNowDate());
        int i = baseMapper.insertLRecharge(lRecharge);
        rechargebill++;
        redisCache.setCacheObject("rechargebill" + str, rechargebill, 1, TimeUnit.DAYS);
        return i;
    }

    //验证订单编号是否重复
    public void checkBillCode(LRecharge recharge) {
        String Billcode = recharge.getBillcode();
        LRecharge param = new LRecharge();
        param.setBillcode(Billcode);
        List<LRecharge> exebills = selectLRechargeList(param);
        if (exebills.size() > 0) {
            String str = getNowDate();
            String type = sysConfigService.selectConfigByKey("recharge.bill.code.type");
            String billCode = type + "-" + str + "%";
            int num = baseMapper.selectCountByBillCode(billCode);
            redisCache.setCacheObject("exebill" + str, num, 1, TimeUnit.DAYS);
            recharge.setBillcode(String.valueOf(getBillCode()));
            checkBillCode(recharge, num);
        }


    }

    public void checkBillCode(LRecharge recharge, int num) {
        String Billcode = recharge.getBillcode();
        LRecharge param = new LRecharge();
        param.setBillcode(Billcode);
        List<LRecharge> exebills = selectLRechargeList(param);
        if (exebills.size() > 0) {
            String str = getNowDate();
            num++;
            redisCache.setCacheObject("exebill" + str, num, 1, TimeUnit.DAYS);
            recharge.setBillcode(String.valueOf(getBillCode()));
            checkBillCode(recharge, num);
        }
    }

    /**
     * 修改充值订单
     *
     * @param lRecharge 充值订单
     * @return 结果
     */
    @Override
    public int updateLRecharge(LRecharge lRecharge) {
        lRecharge.setUpdateBy(String.valueOf(getUserId()));
        lRecharge.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateLRecharge(lRecharge);
    }

    /**
     * 批量删除充值订单
     *
     * @param rechargeids 需要删除的充值订单主键
     * @return 结果
     */
    @Override
    public int deleteLRechargeByRechargeids(Long[] rechargeids) {
        return baseMapper.deleteLRechargeByRechargeids(rechargeids);
    }

    /**
     * 删除充值订单信息
     *
     * @param rechargeid 充值订单主键
     * @return 结果
     */
    @Override
    public int deleteLRechargeByRechargeid(Long rechargeid) {
        return baseMapper.deleteLRechargeByRechargeid(rechargeid);
    }

    @Override
    public Object getBillCode() {

        String str = getNowDate();
        String type = sysConfigService.selectConfigByKey("recharge.bill.code.type");
        String billCode = type + "-" + str + "-";
        int rechargebill = redisCache.getCacheObject("rechargebill" + str) == null ? 0 : redisCache.getCacheObject("rechargebill" + str);
        rechargebill++;
        billCode = getCode(billCode, rechargebill);
        return billCode;
    }
}
